from django.core.cache import cache
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView


# Create your views here.
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from home.ser import TopSer, BannerSer
from home.models import TopModel, BannerModel


# Create your views here.
class TopView(APIView):
    def get(self,request):
        top = TopModel.objects.filter(position=True,isshow=True).order_by("order")
        ser = TopSer(top,many=True)
        return Response({"msg":"success","data":ser.data,"code":200})

class FootView(APIView):
    def get(self,request):
        #先设置一个缓存的key
        key="footview"
        cacheres=cache.get(key)
        #判断缓存中是否存在这个key
        if cacheres:
            return Response({"msg":"success","data":cacheres,"code":200})
        top = TopModel.objects.filter(position=False,isshow=True).order_by("order")
        ser = TopSer(top,many=True)
        cache.set(key,ser.data,60*60*24)
        return Response({"msg":"success","data":ser.data,"code":200})

class BannerView(APIView):
    def get(self,request):
        banner=BannerModel.objects.filter(isshow=True).order_by("order")
        ser = BannerSer(banner,many=True)
        return Response({"msg":"success","data":ser.data,"code":200})